{-# LANGUAGE DeriveDataTypeable #-}

module Xml(

  module Text.XML.HaXml.Types
, module Text.XML.HaXml.Posn
, module Generics.MultiRec.Language.XML
, languagexml
, parseXML

) where

-- container
import Data.Tree (Tree(Node,rootLabel))

-- local imports
import Language.Dependencies.Language
import Language.Dependencies.Term2tree (data2tree)

import Text.XML.HaXml.Types 
import Text.XML.HaXml.Posn
import Text.XML.HaXml.Parse hiding (name)
import Generics.MultiRec.Language.XML
 
languagexml = Language {
    name=  "XML" 
  , syntax = "xml"
  , exts = [".xhtml",".xml",".jsp"] 
  , parse = parseXML
  , toTree = (data2tree TheDocument :: (Document Posn) -> Tree String)
  , srcLoc       = toSrcLoc
  , adjustSrcLoc = Nothing
  , srcLoctype   = (Coordinate undefined undefined) 
  , typerep = Just (TheDocument)
  }
    
parseXML :: String -> Either Error (Document Posn)
parseXML s = case (xmlParse' "unknown" s) of
   Right xml -> Right xml
   Left msg   -> Left (ErrMessage msg)

toSrcLoc :: Tree String -> [Loc]
toSrcLoc (Node l@('f':_) [] ) = return $ Coordinate 
     (fst $ extract $ words l
     ,snd $ extract $ words l) 
     (fst $ extract $ words l
     ,snd $ extract $ words l) 
toSrcLoc _ = []

extract :: [String] -> (Int,Int)
extract [_,_,_,_,line,_,column] = ((read line :: Int),(read column :: Int))
extract _ = (-1,-1)
